//package com.fecean.aabbc.netty.server.prod2;
//
//import io.netty.bootstrap.ServerBootstrap;
//import io.netty.channel.ChannelFuture;
//import io.netty.channel.ChannelOption;
//import io.netty.channel.EventLoopGroup;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.nio.NioServerSocketChannel;
//import io.netty.handler.logging.LogLevel;
//import io.netty.handler.logging.LoggingHandler;
//import io.netty.util.concurrent.DefaultThreadFactory;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.net.InetSocketAddress;
//import java.util.concurrent.ThreadFactory;
//
///**
// * Netty服务端启动服务
// *
// * @author: chenfenghai
// * @version: 1.0
// * @date: 2020-03-10 10:06
// * @blame Team
// */
//@Slf4j
//@Component
//public class NettyServerStarter {
//
//    /**
//     ** 通道初始化
//     */
//    @Resource
//    NettyServerChannelInitializer nettyServerChannelInitializer;
//
//    /**
//     ** Netty服务端启动
//     */
//    public void start(){
//        InetSocketAddress socketAddress = new InetSocketAddress(8090);
//        // new 一个主线程组
//        ThreadFactory bossFactory = new DefaultThreadFactory("netty.boss");
//        EventLoopGroup bossGroup = new NioEventLoopGroup(bossFactory);
//        // new 一个工作线程组
//        ThreadFactory workerFactory = new DefaultThreadFactory("netty.worker");
//        EventLoopGroup workGroup = new NioEventLoopGroup(workerFactory);
//        try {
//            // netty服务端应用入口
//            ServerBootstrap bootstrap = new ServerBootstrap()
//                    .group(bossGroup, workGroup)
//                    .channel(NioServerSocketChannel.class)
//                    .handler(new LoggingHandler(LogLevel.INFO))
//                    .localAddress(socketAddress)
//                    .childHandler(nettyServerChannelInitializer)
//                    // 设置队列大小
//                    .option(ChannelOption.SO_BACKLOG, 1024)
//                    // 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文
//                    .childOption(ChannelOption.SO_KEEPALIVE, true);
//
//            // 绑定端口,开始接收进来的连接
//            ChannelFuture future = bootstrap.bind(socketAddress).sync();
//            log.info("Netty服务端启动成功，开始监听端口: {}", socketAddress.getPort());
//            future.channel().closeFuture().sync();
//        } catch (Exception e) {
//            log.error("Netty服务端启动失败", e);
//        } finally {
//            //关闭主线程组
//            bossGroup.shutdownGracefully();
//            //关闭工作线程组
//            workGroup.shutdownGracefully();
//        }
//    }
//}
